package Model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import view.TimerBacaPublikasi;
import view.TimerSimpanPublikasi;


public class Perpustakaan {
    public static String Nama = "Perpustakaan 115314034";
    public static ArrayList <Publikasi> DAFTAR_PUBLIKASI = new ArrayList<>();
    public static ArrayList <Anggota> DAFTAR_ANGGOTA = new ArrayList<>();
    public static ArrayList <Pinjaman> DAFTAR_PINJAMAN = new ArrayList<>();
    
    public static ArrayList <TimerBacaPublikasi> TIMER_BACA_PUBLIKASI  = new ArrayList<>();
    public static ArrayList <TimerSimpanPublikasi> TIMER_SIMPAN_PUBLIKASI  = new ArrayList<>();
    
    public static ArrayList <Anggota> cariAnggota(String nama){
        ArrayList<Anggota>hasil=new ArrayList<Anggota>();
        for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
           Anggota temp= DAFTAR_ANGGOTA.get(i);
           if(temp.getNama().contains(nama)){
               hasil.add(temp);
           }
            
        }
        if(hasil.isEmpty()){
            JOptionPane.showMessageDialog(null, "Data Tidak Ditemukan");
        }
        return hasil;
    }
    public static void tambahPublikasi( Publikasi inPublikasi){
    DAFTAR_PUBLIKASI.add(inPublikasi);
    }
    public static void tambahAnggota(Anggota inAnggota){
    DAFTAR_ANGGOTA.add(inAnggota);
    }
    public static void tambahPinjaman(Pinjaman inPinjaman){
    DAFTAR_PINJAMAN.add(inPinjaman);
    }
    
    public static String bacaDaftarPublikasi(){
    if(DAFTAR_PUBLIKASI.isEmpty()== false){
    String hasil = new String();
    for (int i = 0; i < 10; i++) {
    if(i < DAFTAR_PUBLIKASI.size()){
    hasil += "Judul      : "+DAFTAR_PUBLIKASI.get(i).getJudul() + "\n";
    hasil += "Id Koleksi : "+DAFTAR_PUBLIKASI.get(i).getIdkoleksi() + "\n";
    hasil += "Penulis    : "+DAFTAR_PUBLIKASI.get(i).getPenulis() + "\n";
    hasil += "Penerbit   : "+DAFTAR_PUBLIKASI.get(i).getPenerbit() + "\n";
    hasil += "Tahun      : "+DAFTAR_PUBLIKASI.get(i).getTahun() + "\n";
    } else{
    return hasil;
    }}}
    return "kosong";
    }
    public static String bacaDaftarAnggota(){
    if(DAFTAR_ANGGOTA.isEmpty()== false){
    String hasil = new String();
    for (int i = 0; i < 10; i++) {
    if(i < DAFTAR_ANGGOTA.size()){
    hasil += "Nama         : "+DAFTAR_ANGGOTA.get(i).getNama() + "\n";
    hasil += "Id Anggota   : "+DAFTAR_ANGGOTA.get(i).getIdAnggota();
    }
    else{
    return hasil;
    }}}
    return "kosong";
    }
    public static String bacaDaftarPinjaman(){
    if(DAFTAR_PINJAMAN.isEmpty()== false){
    String hasil = new String();
    for (int i = 0; i < 10; i++) {
    if(i < DAFTAR_PINJAMAN.size()){
    hasil += "Nama         : "+DAFTAR_PINJAMAN.get(i).getPeminjam().getNama() + "\n";
    hasil += "Id Anggota   :"+DAFTAR_PINJAMAN.get(i).getPeminjam().getIdAnggota() + "\n";
    hasil += "Judul        :"+DAFTAR_PINJAMAN.get(i).getPublikasi().getJudul() + "\n";
    hasil += "Id Koleksi   :"+DAFTAR_PINJAMAN.get(i).getPublikasi().getIdkoleksi() + "\n";
    hasil += "Penulis      :"+DAFTAR_PINJAMAN.get(i).getPublikasi().getPenulis() + "\n";
    hasil += "Penerbit     :"+DAFTAR_PINJAMAN.get(i).getPublikasi().getPenerbit() + "\n";
    hasil += "Tanggal Pinjam :"+DAFTAR_PINJAMAN.get(i).getTanggalPinjam().getDate() + "\n";
    hasil += "Tanggal Kembali :"+DAFTAR_PINJAMAN.get(i).getTanggalKembali().getDate() + "\n";
    hasil += "Tahun        :"+DAFTAR_PINJAMAN.get(i).getPublikasi().getTahun() + "\n";
    }
    else{
    return hasil;
    }}}
    return "kosong";
    }
    
    
     public  static void simpanFilePublikasi() throws IOException{
             FileOutputStream Spblks = null;
             if(DAFTAR_PUBLIKASI.isEmpty()==true){
                 JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
             }else{
                 File simpan = new File("Publikasi.dat");
                 Spblks=new FileOutputStream(simpan);
                 ObjectOutputStream obj = new ObjectOutputStream(Spblks);
                 obj.writeObject(DAFTAR_PUBLIKASI);
                 Spblks.close();
             }
         }
         public  static void bacaFilePublikasi() throws FileNotFoundException, IOException, ClassNotFoundException{
             FileInputStream baca = null;
             File read =new File("Publikasi.dat");
             baca = new FileInputStream(read);
             ObjectInputStream obj = new ObjectInputStream(baca);
             DAFTAR_PUBLIKASI = (ArrayList<Publikasi> )obj.readObject();
         }
         
         public static void simpanFileAnggota() throws IOException {
        FileOutputStream outStream = null;
        if (DAFTAR_ANGGOTA.isEmpty()){
            JOptionPane.showMessageDialog(null,"Daftar Anggota Kosong");
        }
    else {

            try {
                File outFile = new File("anggota.dat");
                outStream = new FileOutputStream(outFile);
                String data = new String();
                for(int i=0; i<DAFTAR_ANGGOTA.size();i++) {
                    data += "Id Anggota : " + DAFTAR_ANGGOTA.get(i).getIdAnggota() + "\tNama : " + DAFTAR_ANGGOTA.get(i).getNama() + "\n";
                }
                outStream.write(data.getBytes());
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    }
    public static void bacaFileAnggota()  {
        try {
            FileInputStream inStream = null;
            String data = new String();
            File inFile = new File("anggota.dat");
            try {
                inStream = new FileInputStream(inFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            int a;
            while ((a = inStream.read()) != -1) {
                data = data + (char) a;
            }
            inStream.close();
            System.out.println(data);
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

public static void simpanFilePinjaman() throws IOException {
        FileOutputStream outStream = null;
        if (DAFTAR_PINJAMAN.isEmpty()){
            JOptionPane.showMessageDialog(null,"Daftar Pinjaman Kosong");
        }
    else {

            try {
                File outFile = new File("pinjaman.dat");
                outStream = new FileOutputStream(outFile);
                String data = new String();
                for(int i=0; i<DAFTAR_PINJAMAN.size();i++) {
                    data += "Id Anggota : " + DAFTAR_PINJAMAN.get(i).getPeminjam().getIdAnggota() + "\tId Koleksi : " + DAFTAR_PINJAMAN.get(i).getPublikasi().getIdkoleksi() + "\n";
                }
                outStream.write(data.getBytes());
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    }
    public static void bacaFilePinjaman()  {
        try {
            FileInputStream inStream = null;
            String data = new String();
            File inFile = new File("pinjaman.dat");
            try {
                inStream = new FileInputStream(inFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            int a;
            while ((a = inStream.read()) != -1) {
                data = data + (char) a;
            }
            inStream.close();
            System.out.println(data);
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
        public static void tambahTabelPublikasi(Publikasi publikasi){
          Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314034","MHS115314034");
            Statement statement = null;
        try {
            statement = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
            String sql = "insert into publikasi (id_koleksi, judul) values ('"+publikasi.getIdkoleksi() + "','" + publikasi.getJudul()+"')";
        try {
            statement.executeUpdate(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
         
     }
        public static void bacaTabelPublikasi(){
       DAFTAR_PUBLIKASI= new ArrayList<Publikasi>();
       Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314034", "MHS115314034");
        java.sql.Statement statement = null;
        try {
            statement = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
       String sql = "select * from publikasi";
       ResultSet result = null;
        try {
            result = statement.executeQuery(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while (result.next()) {
                Publikasi temp = new Publikasi() {

                    public int hitungDenda(int lamaPinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                };
                temp.setIdkoleksi(result.getString("id_koleksi"));
                temp.setJudul(result.getString("judul"));
                temp.setPenulis(result.getString("penulis"));
                temp.setPenerbit(result.getString("penerbit"));
                temp.setTahun(result.getInt("tahun"));
                DAFTAR_PUBLIKASI.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
   }

         public static void tambahTabelAnggota (Anggota anggota){
        try {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314034", "MHS115314034");
            java.sql.Statement statement = null;
            statement = conn.createStatement();
            String sql = "insert into anggota (id_anggota,nama) values ('" + anggota.getIdAnggota() + "','" + anggota.getNama() + "')";
            statement.executeUpdate(sql);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

         public static void bacaTabelAnggota(){
       DAFTAR_ANGGOTA= new ArrayList<Anggota>();
       Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314034", "MHS115314034");
        java.sql.Statement statement = null;
        try {
            statement = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
       String sql = "select * from anggota";
       ResultSet result = null;
        try {
            result = statement.executeQuery(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while (result.next()) {
                Anggota temp = new Anggota() {

                    public int hitungDenda(int lamaPinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                };
                temp.setIdAnggota(result.getString("id_anggota"));
                temp.setNama(result.getString("nama"));
                DAFTAR_ANGGOTA.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
   }
         
          public static void bacaTabelPinjaman(){
       DAFTAR_PINJAMAN= new ArrayList<Pinjaman>();
       Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314034", "MHS115314034");
        java.sql.Statement statement = null;
        try {
            statement = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
       String sql = "select * from pinjaman";
       ResultSet result = null;
        try {
            result = statement.executeQuery(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while (result.next()) {
                Pinjaman temp = new Pinjaman();

                Anggota agg = new Anggota();
                Publikasi pub = new Publikasi() {

                    public int hitungDenda(int lamaPinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                };

                agg.setIdAnggota(result.getString("id_anggota"));
                pub.setIdkoleksi(result.getString("id_koleksi"));
                temp.setPeminjam(agg);
                temp.setPublikasi(pub);
                DAFTAR_PINJAMAN.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
   }


          public static void tambahTabelPinjaman (Pinjaman pinjaman){
        try {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314034", "MHS115314034");
            java.sql.Statement statement = null;
            statement = conn.createStatement();
            String sql = "insert into pinjaman (id_anggota, id_koleksi) values ('" + pinjaman.getPeminjam().getIdAnggota() + "','" + pinjaman.getPublikasi().getIdkoleksi() + "')";
            statement.executeUpdate(sql);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

          public static void simpanTabelPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null,"Daftar Publikasi Kosong");
        } else {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314034", "MHS115314034");
            try {
                java.sql.Statement statement = conn.createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (int i=0; i<DAFTAR_PUBLIKASI.size(); i++){
                String sql = "insert into publikasi (id_koleksi,judul,penulis,penerbit,tahun) values ('"+DAFTAR_PUBLIKASI.get(i).getIdkoleksi() + "','" + DAFTAR_PUBLIKASI.get(i).getJudul()+
                        DAFTAR_PUBLIKASI.get(i).getPenulis() + "','" + DAFTAR_PUBLIKASI.get(i).getPenerbit() + "','" +
                        DAFTAR_PUBLIKASI.get(i).getTahun() +"')";
            }
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

          public static void simpanTabelAnggota() {
        if (DAFTAR_ANGGOTA.isEmpty()) {
            JOptionPane.showMessageDialog(null,"Daftar Anggota Kosong");
        } else {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314034", "MHS115314034");
            try {
                java.sql.Statement statement = conn.createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (int i=0; i<DAFTAR_ANGGOTA.size(); i++){
                String sql = "insert into anggota (id_anggota,nama) values ('"+DAFTAR_ANGGOTA.get(i).getIdAnggota() + "','" + DAFTAR_ANGGOTA.get(i).getNama()+ "')";
            }
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

          public static void simpanTabelPinjaman() {
        if (DAFTAR_PINJAMAN.isEmpty()) {
            JOptionPane.showMessageDialog(null,"Daftar Pinjaman Kosong");
        } else {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314034", "MHS115314034");
            try {
                java.sql.Statement statement = conn.createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (int i=0; i<DAFTAR_PINJAMAN.size(); i++){
                String sql = "insert into pinjaman (id_anggota, id_koleksi) values ('"+DAFTAR_PINJAMAN.get(i).getPeminjam().getIdAnggota() + "','" + DAFTAR_PINJAMAN.get(i).getPublikasi().getIdkoleksi();
            }
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
   
//         public static  ArrayList<Anggota> cariAnggota(String nama) {
//            ArrayList<Anggota> hasil = new ArrayList<Anggota>();
//            for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
//            Anggota temp = DAFTAR_ANGGOTA.get(i);
//            if (temp.getNama().contains(nama)) {
//                hasil.add(temp);
//            }
//         
//        }
//            return hasil;
//    }
    }
